//  基本信息
var baseInfoComponent = Vue.extend({
  template: '#baseInfo',
  data: function() {
    return {
      user: {},
      userNews: {},
      editting: false,
      loginer: {}
    }
  },
  mounted: function() {
    var user = localStorage.memoryClassUser
    if (user) {
      this.loginer = JSON.parse(user)
    }

    this.getUser()

  },
  methods: {
    getUser: function() {
      if (this.user.id) {
        return
      }

      this.$http.get('/user/baseInfo/id/' + $$.getQuery('id')).then(function(res) {
        if (res.data.code == 100) {
          this.user = res.data.data.user
          this.userNews = $$.extend({}, this.user)
          this.$emit('set-user', this.user)

        } else {
          $$.alert(res.data.msg)
        }
      })
    },
    saveEdit: function() {
      var userNews = this.userNews
      if (!$$.trim(userNews.name)) {
        return $$.alert('用户名不能为空!')
      }
      this.$http.put('/user/baseInfo/id/' + $$.getQuery('id'), {
        user: userNews
      }).then(function(res) {
        if (res.data.code == 100) {
          this.user = $$.extend({}, userNews)
          this.$nextTick(function() {
            this.editting = false
          })
        } else {
          $$.alert(res.data.msg)
        }
      })
    }
  }

})



//  训练中心
var testCenterComponent = Vue.extend({
  template: '#testCenter',
  data: function() {
    return {
      page: 1,
      list: []
    }
  },
  mounted: function() {

    this.getTest(0)
  },
  methods: {
    getTest: function(num) {

      this.$http.get('/test/type/2/page/' + (this.page + num), {
        params: {
          userId: $$.getQuery('id')
        }
      }).then(function(res) {
        console.log(res.data)
        if (res.data.code == 100) {
          this.list = res.data.data.list
          this.page = this.page + num
        } else if (num != 0) {
          if (res.data.code == 112) {
            $$.alert('已经是最后一页啦')
          } else {
            $$.alert(rea.data.msg)
          }
        }
      })

    }
  },
  props: ['loginer']
})


//  我的帖子
var postCenterComponent = Vue.extend({
  template: '#postCenter',
  data: function() {
    return {
      list: [],
      page: 1
    }
  },
  mounted: function() {
    this.$nextTick(function() {
      this.getPost(0)
    })
  },
  methods: {
    getPost: function(num) {

      this.$http.get('/post/page/' + (this.page + num), {
        params: {
          userId: this.$parent.userId
        }
      }).then(function(res) {
        if (res.data.code == 100) {
          this.list = res.data.data.list
          this.page += num
        } else if (num != 0) {
          if (res.data.code == 112) {
            $$.alert('已经最后一页了')
          } else {
            $$.alert(res.data.msg)
          }
        }
      })

    }
  }
})


//  我的动态
var myActiveComponent = Vue.extend({
  template: '#myActive',
  data: function() {
    return {
      list: []
    }
  },
  mounted: function() {
    this.$nextTick(function() {
      this.$http.get('/active', {
        params: {
          userId: this.$parent.userId
        }
      }).then(function(res) {
        if (res.data.code == 100) {
          this.list = res.data.data.list
        } else {
          console.log(res.data.msg)
        }
      })
    })

  }

})

//  安全中心
var safeCenterComponent = Vue.extend({
  template: '#safeCenter',
  data: function() {
    return {
      bindEmailInfo: {
        patt: /^[\w\-]+@[0-9a-zA-Z]{2,}(\.[a-zA-Z]{2,3}){1,2}$/,
        email: '',
        verifyCode: '',
        disabled: false,
        getVerifyCodeBtnText: '获取验证码'
      },
      changePassword: {
        oldPassword: '',
        newPassword: '',
        confirmPassword: ''
      }
    }
  },
  computed: {
    isChangeBindEmail: function() {
      return !!this.user.email
    }
  },
  methods: {
    sendVerifyCode: function() {
      if (!this.bindEmailInfo.patt.test(this.bindEmailInfo.email)) {
        return $$.alert('邮箱格式不正确')
      }
      this.$http.get('/user/verifyCode', {
        params: {
          email: this.bindEmailInfo.email
        }
      }).then(function(res) {
        if (res.data.code == 100) {
          $$.alert('验证码已发送至您的邮箱')
          this.sendVerifyCodeBtnTimeout()
        } else {
          $$.alert(res.data.msg)
        }
      })
    },
    //  绑定邮箱和改绑邮箱函数
    bindEmail: function() {

      if (!this.bindEmailInfo.patt.test(this.bindEmailInfo.email)) {
        return alert('邮箱格式不正确')
      }
      if (!this.bindEmailInfo.verifyCode || this.bindEmailInfo.verifyCode.length != 4) {
        return alert('请输入正确验证码')
      }
      var newUser = $$.extend({}, this.user, {
        email: this.bindEmailInfo.email
      })
      var options = {
        verifyCode: this.bindEmailInfo.verifyCode,
        user: newUser
      }
      if (this.isChangeBindEmail) {
        options.isChangeBindEmail = true
        options.password = this.bindEmailInfo.password
      }

      this.$http.put('/user/baseInfo/id/' + $$.getQuery('id'), options).then(function(res) {
        if (res.data.code == 100) {
          alert('绑定成功')
          this.user = res.data.data.user
          location.reload()
        } else {
          alert(res.data.msg)
        }
      })
    },
    sendVerifyCodeBtnTimeout: function() {
      this.bindEmailInfo.disabled = true
      var num = 60
      var that = this
      var timer = setInterval(function() {
        if (--num < 0) {
          that.bindEmailInfo.getVerifyCodeBtnText = '获取验证码'
          that.bindEmailInfo.disabled = false
          clearInterval(timer)
          return
        }
        that.bindEmailInfo.getVerifyCodeBtnText = num + '秒'
      }, 1000)
    },

    saveNewPassword: function() {
      var tips = this.changePassword
      if (!$$.trim(tips.oldPassword) || !$$.trim(tips.newPassword) || !$$.trim(tips.confirmPassword)) {
        return $$.alert('请填写完整')
      }
      if (tips.newPassword != tips.confirmPassword) {
        return $$.alert('两次新密码不一致')
      }
      this.$http.get('/user/login', {
        params: {
          name: this.user.name,
          password: tips.oldPassword
        }
      }).then(function(res) {
        if (res.data.code == 100) {
          //  保存新密码
          this.$http.put('/user/password/id/' + $$.getQuery('id'), {
            newPassword: tips.newPassword,
            user: this.user
          }).then(function(res) {
            if (res.data.code == 100) {
              $$.alert('密码修改成功')
              location.reload()
            } else {
              $$.alert(res.data.msg)
            }
          })

        } else {
          $$.alert('旧密码错误')
        }
      })
    },
    forgetPassword: function() {
      $$.alert('暂未开放')
    }
  },
  props: ['user']
})

//  网站管理
var applyManageComponent = Vue.extend({
  template: '#applyManage',
  data: function() {
    return {
      tabIndex: 0,
      applyManageRemark: '',
      suggestRemark: ''
    }
  },
  methods: {
    submitAppply: function() {
      if (this.applyManageRemark.length < 20) {
        return $$.alert('申请理由至少20字')
      }
      this.$http.post('/user/applyManage', {
        handler: this.$parent.loginer,
        content: this.applyManageRemark
      }).then(function(res) {
        if (res.data.code == 100) {
          $$.alert('申请成功')
          this.applyManageRemark = ''
        } else {
          $$.alert(res.data.msg)
        }
      })

    },
    submitSuggest: function() {
      if (!this.suggestRemark) {
        return $$.alert('请输入投诉或建议内容')
      }
      this.$http.post('/user/suggest', {
        handler: this.$parent.loginer,
        content: this.suggestRemark
      }).then(function(res) {
        if (res.data.code == 100) {
          $$.alert('提交成功')
          this.suggestRemark = ''
        } else {
          $$.alert(res.data.msg)
        }
      })
    }
  }
})


//  退出登录
var exitLoginComponent = Vue.extend({
  template: '#exitLogin',
  data: function() {
    return {

    }
  },
  methods: {
    cancelExit: function() {
      this.$emit('go-to-base-info')
    },
    exit: function() {
      localStorage.memoryClassUser = ''
      location = 'http://' + location.host
    }
  }
})